home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
56194
/
56194.xpi
/
chrome
/
topsite.jar
/
content
/
js
/
toolbarView.js
< prev
next >
Wrap
Text File
|
2010-01-17
|
8KB
|
220 lines
if (!topsite) var topsite={};
topsite.toolbarView = function(topicsContainer, getSitesFunc, onSiteClickFunc) {
this.topicsContainer = topicsContainer;
this.chevron = topicsContainer.parentNode.lastChild;
this.getSitesFunc = getSitesFunc;
this.onSiteClickFunc = onSiteClickFunc;
this.populate = function(arrTags){
for (var i = 0; i < arrTags.length; ++i){
var strTag = arrTags[i];
strTag = topsite.common.capitalizeWords(strTag);
var newToolbarButton = document.createElement("toolbarbutton");
newToolbarButton.setAttribute("label", strTag);
newToolbarButton.setAttribute("class", "relatedTopicButton");
newToolbarButton.setAttribute("type", "menu");
newToolbarButton.setAttribute("image", "chrome://topsite/content/images/category" + i + ".png");
this.topicsContainer.appendChild(newToolbarButton);
var newMenuInButton = document.createElement("menupopup");
newToolbarButton.appendChild(newMenuInButton);
var myView = this;
newToolbarButton.addEventListener("popupshowing", function f(event){
myView.getSitesFunc(event.target.parentNode.getAttribute("label"), event.target);
}, false);
var newMenuItemInChevron = document.createElement("menu");
newMenuItemInChevron.setAttribute("label", strTag);
newMenuItemInChevron.setAttribute("tooltipText", "")
newMenuItemInChevron.setAttribute("class", "relatedTopicMenuButton menu-iconic");
newMenuItemInChevron.setAttribute("image", "chrome://topsite/content/images/category" + i + ".png");
if (this.chevron.firstChild)
this.chevron.firstChild.appendChild(newMenuItemInChevron);
else
this.chevron.appendChild(newMenuItemInChevron);
var newMenuInMenuButton = document.createElement("menupopup");
newMenuItemInChevron.appendChild(newMenuInMenuButton);
newMenuItemInChevron.addEventListener("popupshowing", function f(event){
myView.getSitesFunc(event.target.parentNode.getAttribute("label"), event.target);
}, false);
newMenuItemInChevron.collapsed = true;
}
if (arrTags.length > 0) {
this.topicsContainer.previousSibling.collapsed = false;
} else {
var newToolbarButton = document.createElement("toolbarbutton");
newToolbarButton.setAttribute("label", "No Results: Try Your Own");
newToolbarButton.setAttribute("class", "relatedTopicButton");
newToolbarButton.setAttribute("type", "button");
newToolbarButton.setAttribute("image", "chrome://topsite/content/images/category0.png");
newToolbarButton.setAttribute("oncommand", "topsite.overlay.openUrlInBrowser('http://www.topsite.com/?r=noresults');");
this.topicsContainer.appendChild(newToolbarButton);
}
var t = this;
setTimeout(function(){t.updateChevron();},200);
}
this.clear = function(){
while (this.topicsContainer.firstChild){
this.topicsContainer.removeChild(this.topicsContainer.firstChild);
if (this.chevron.firstChild && this.chevron.firstChild.firstChild)
this.chevron.firstChild.removeChild(this.chevron.firstChild.firstChild);
}
this.topicsContainer.previousSibling.collapsed = true;
this.chevron.collapsed = true;
}
this.showTopSite = function(arrTopSite, elmMenu){
if (elmMenu.childNodes.length > 0)
return;
var originalStrTopic = elmMenu.parentNode.getAttribute("label");
var strTopic = topsite.common.capitalizeWords(originalStrTopic);
var titleItem = document.getElementById("topSitePopup4TopicTitle").cloneNode(true);
titleItem.lastChild.setAttribute("value", topsite.consts.STRING_BUNDLE.GetStringFromName("popup.title").replace("%S", strTopic));
elmMenu.appendChild(titleItem);
elmMenu.appendChild(document.createElement("menuseparator"));
for (var i = 0; i < arrTopSite.length; ++i){
var strDisplayUrl = arrTopSite[i][0];
var strNavUrl = arrTopSite[i][1];
var isAdv = arrTopSite[i][2];
var newMenuItem = document.createElement("menuitem");
newMenuItem.setAttribute("label", strDisplayUrl);
newMenuItem.setAttribute("url", strNavUrl);
newMenuItem.setAttribute("class", "menuitem-iconic");
newMenuItem.setAttribute("isAdv", isAdv);
elmMenu.appendChild(newMenuItem);
var myView = this;
newMenuItem.onclick = function(event){
myView.onSiteClickFunc(event.target.getAttribute("url"), originalStrTopic, event.target.getAttribute("isAdv"));
}
this.addFavIcon(strDisplayUrl, newMenuItem, "image", "chrome://topsite/content/images/noicon.png");
}
if (arrTopSite.length==0) {
var newMenuItem = document.createElement("menuitem");
newMenuItem.setAttribute("label", "No top sites found, please try again later.");
newMenuItem.setAttribute("class", "menuitem-iconic");
elmMenu.appendChild(newMenuItem);
}
elmMenu.appendChild(document.createElement("menuseparator"));
var footerItem = document.getElementById("topSitePopup4TopicFooter").cloneNode(true);
elmMenu.appendChild(footerItem);
footerItem.firstChild.onclick = function(event){
topsite.overlay.findTopSiteForTerm(strTopic);
}
}
this.init = function(){
var t = this;
window.addEventListener("resize",
function f(e) { t.updateChevron(e); }, false);
this.chevron.addEventListener("popupshowing", function f2(e2) { t.chevronPopupShowing(e2); }, false);
}
this.chevronPopupShowing = function(aEvent){
var popup = aEvent.target;
if (popup != this.chevron.firstChild)
return;
for (var i = 0; i < popup.childNodes.length; i++)
popup.childNodes[i].collapsed = !this.topicsContainer.childNodes[i].collapsed;
}
this.getElementWidth = function(element){
var style = document.defaultView.getComputedStyle(element, "");
var leftMargin = style.getPropertyValue("margin-left");
leftMargin = leftMargin ? Math.round(parseFloat(leftMargin)) : 0;
var rightMargin = style.getPropertyValue("margin-right");
rightMargin = rightMargin ? Math.round(parseFloat(rightMargin)) : 0;
return element.boxObject.width + leftMargin + rightMargin;
}
this.updateChevron = function(event){
if (event && event.target != document && event.target != window && event.target != content)
return;
if (this.topicsContainer.childNodes.length == 0) {
this.chevron.collapsed = true;
return;
}
var spaceLeft = this.topicsContainer.boxObject.width;
this.chevron.collapsed = false;
var chevronWidth = this.chevron.boxObject.width;
var overflowed = false;
for (var i = 0; i < this.topicsContainer.childNodes.length; i++) {
var child = this.topicsContainer.childNodes[i];
child.collapsed = false;
spaceLeft -= this.getElementWidth(child);
var spaceNeeded = (i == this.topicsContainer.childNodes.length - 1) ? 0 : chevronWidth;
if (spaceLeft < spaceNeeded) {
overflowed = true;
child.collapsed = true;
}
}
this.chevron.collapsed = !overflowed;
}
this.addFavIcon = function(linkUrl, menuItem, attr, noImage){
var cuesrc = this.getUrlPrePath(linkUrl) + "/favicon.ico";
menuItem.onerror = function () {
this.setAttribute(attr, noImage);
}
menuItem.setAttribute(attr, cuesrc);
}
this.getUrlPrePath = function(url){
try {
var fullUrl = url;
if (fullUrl.indexOf("://") < 0){
if (fullUrl.indexOf(".") == fullUrl.lastIndexOf("."))
fullUrl = "www." + fullUrl;
fullUrl = "http://" + fullUrl;
}
var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
var uri = ios.newURI(fullUrl, null, null);
return uri.prePath;
}
catch (e) {
return url;
}
}
this.init();
}
topsite.toolbarView.prototype = new topsite.view();
topsite.toolbarView.prototype.constructor = topsite.view;